﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ServerSide.Common;
using ServerSide.EFCoreContext;
using ServerSide.Model;
using ServerSide.Model.Dto.MenuDto;
using ServerSide.Service.Interface;


namespace ServerSide.Controllers
{
    [ApiController]
    [Route("menuapi/[action]")]
    public class MenuController
    {
        private readonly MyDBContext _myDBContext;
        private readonly IMenuService _menuService;
        private readonly IRoleService _roleService;
        public MenuController(MyDBContext myDBContext,IMenuService menuService, IRoleService roleService)
        {
            _myDBContext = myDBContext;
            _menuService = menuService;
            _roleService = roleService;
        }

        /// <summary>
        /// 菜单管理页面-获取菜单信息
        /// </summary>
        /// <param name="menuRequest"></param>
        /// <returns></returns>
        [HttpPost(Name = "getMenuData")]
        public Result<Object> GetMenuData(MenuRequest menuRequest)
        {
            List<Menu> menus = _menuService.GetMenus(menuRequest);
            if (menus != null)
            {
                return Result<Object>.Success(menus);
            }
            return Result<Object>.Error("501", "获取失败");
        }

        /// <summary>
        /// 角色管理页面-获取菜单权限信息
        /// </summary>
        /// <param name="menuRequest"></param>
        /// <returns></returns>
        [HttpGet(Name = "getMenuTreeSelect")]
        public Result<Object> GetMenuTreeSelect(int roleId)
        {
            List<Menu> menus = _menuService.SelectMenuList();
            List<int> checkKeys = _roleService.GetMenuIdByRoleId(roleId);

            if (menus != null)
            {
                return Result<Object>.Success(
                    new
                    {
                        checkKeys,
                        menus
                    }
             );
            }
            return Result<Object>.Error("501", "获取失败");
        }
        /// <summary>
        /// 菜单管理页面-添加菜单信息
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPost(Name = "addMenu")]
        public Result<Object> AddMenu(MenuAdd menuAdd)
        {
            bool result =_menuService.AddMenu(menuAdd);
            if (result)
            {
                return Result<Object>.Success("添加成功");
            }
            return Result<Object>.Error("501", "添加失败");
        }
        /// <summary>
        /// 菜单管理页面-修改菜单信息
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPost(Name = "updateMenuData")]
        public Result<Object> UpdateMenuData(Menu menu)
        {
            bool result = _menuService.UpdateMenu(menu);
            if (result)
            {
                return Result<Object>.Success("修改菜单成功");
            }
            return Result<Object>.Error("501", "修改菜单失败");

        }

        /// <summary>
        /// 菜单管理页面-修改页面是否保持缓存信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="enable"></param>
        /// <returns></returns>
        [HttpGet(Name = "changeMenuStatus")]
        public Result<Object> ChangeMenuStatus(int id, int keepAlive)
        {
            bool v = _menuService.UpdateMenuKeepAlive(id, keepAlive);
            if (v)
            {
                return Result<Object>.Success("修改菜单状态成功");
            }
            return Result<Object>.Error("501", "修改菜单状态失败");
        }

        /// <summary>
        /// 菜单管理页面-删除菜单信息
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPost(Name = "deleteMenu")]
        public Result<Object> DeleteMenu(Menu menu)
        {
            bool result = _menuService.DelMenu(menu);
            if (result)
            {
                return Result<Object>.Success("删除菜单成功");
            }
            return Result<Object>.Error("501", "删除菜单失败");
        }

        /// <summary>
        /// 菜单管理页面-批量删除页面
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost(Name = "batchDelMenu")]
        public Result<Object> BatchDelMenu(Object obj)
        {
            List<int>? ids = JsonConvert.DeserializeObject<List<int>>(obj.ToString());
            bool result = _menuService.BatchDelMenus(ids);
            if (result)
            {
                return Result<Object>.Success("批量删除菜单成功");
            }
            return Result<Object>.Error("501", "批量删除菜单失败");
        }
    }
}
